<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>分项工程管理</title>
    <meta name="description" content="分项工程管理">
    <meta name="keywords" content="分项工程管理">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../../font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <!-- 主体部分 -->
    <div class="app-container">
        <div class="box">
            <!-- 新建组件 -->
            <div class="filter-container">
                <el-input placeholder="承包商搜索" clearable v-model="pagination.queryString" style="width: 300px;"
                          class="filter-item">
                    <el-button slot="append" @click="findPage()" icon="el-icon-search"></el-button>
                </el-input>
                <el-button type="primary" class="butT" @click="handleAdd()">新建</el-button>
            </div>

            <!--
                :data 显示数据，数据类型为数组，作用：将dataList数组内的数据循环遍历输出
             -->
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="sname" label="分项工程" align="center"></el-table-column>
                <el-table-column prop="starttime" label="开始时间" align="center"></el-table-column>
                <el-table-column prop="endtime" label="结束结束" align="center"></el-table-column>
                <el-table-column prop="project.pname" label="工程" align="center"></el-table-column>
                <el-table-column prop="updatetime" label="更新时间" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="subitemDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页效果及分页操作显示 -->
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增工程标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增分项工程" :visible.sync="dialogFormVisible">

                    <el-form ref="dataAddForm" status-icon :rules="rules" :model="form" label-width="80px">
                        <el-form-item label="分项工程" prop="sname">
                            <el-col :span="12">
                                <el-input v-model="form.sname"></el-input>
                            </el-col>
                        </el-form-item>
                        <el-form-item label="开始时间" prop="starttime">
                            <el-col :span="12">
                                <el-date-picker type="date" placeholder="选择日期" v-model="form.starttime"
                                                style="width: 100%;"></el-date-picker>
                            </el-col>
                        </el-form-item>
                        <el-form-item label="结束时间" prop="endtime">
                            <el-col :span="12">
                                <el-date-picker type="date" placeholder="选择日期" v-model="form.endtime"
                                                style="width: 100%;"></el-date-picker>
                            </el-col>
                        </el-form-item>
                        <el-form-item label="工程">
                            <el-col :span="12">
                                <el-select v-model="form.bid" placeholder="请选择工程">
                                    <el-option
                                            v-for="item in form.option1"
                                            :key="item.pid"
                                            :label="item.pname"
                                            :value="item.pid">
                                    </el-option>
                                </el-select>
                            </el-col>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="subitemAdd()">立即创建</el-button>
                            <el-button @click="dialogFormVisible = false">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="修改分项工程" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :rules="rules" :model="formData" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="分项工程" prop="sname">
                                    <el-input v-model="formData.sname"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <!-- 选择项目时间 -->
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="开始时间" prop="starttime">
                                    <el-date-picker v-model="formData.starttime" type="datetime"
                                                    placeholder="选择日期"></el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="结束时间" prop="endtime">
                                    <el-date-picker v-model="formData.endtime" type="datetime"
                                                    placeholder="选择日期"></el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="subitemUpdate()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>

</div>
</body>
<!-- 引入组件库 -->
<script src="../../js/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script type="text/javascript" src="../../js/jquery.min.js"></script>
<script src="../../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data() {
            //校验时间规则：工程开始时间不能比结束时间大
            var checkendtime = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('结束时间不能为空'));
                } else {
                    var endtime = this.form.endtime
                    var starttime = this.form.starttime
                    var formData1 = new Date(starttime)
                    var formData2 = new Date(endtime)
                    if(formData1.getFullYear() > formData2.getFullYear()){
                        console.log(formData1.getFullYear)
                        callback(new Error('开始年份不能大于结束年份'))
                    }else if(formData1.getMonth() > formData2.getMonth()){
                        console.log(formData1.getMonth())
                        callback(new Error('开始月份不能大于结束月份'))
                    }else if(formData1.getDay() > formData2.getDay()){
                        console.log(formData1.getDay())
                        callback(new Error('开始日期不能大于结束日期'))
                    }else{
                        callback();
                    }
                }
            };
            return {
                pagination: {                    //分页相关模型数据
                    currentPage: 1,              //当前页码
                    pageSize: 10,                //每页显示的记录数
                    total: 0,                    //总记录数
                    queryString: null            //查询条件
                },
                dataList: [],                    //当前页要展示的分页列表数据
                formData: {},                    //表单数据
                form: {
                    sname: '',
                    starttime: '',
                    endtime: '',
                    option1: [],
                    pid: ''
                },
                optionList1: [],
                dialogFormVisible: false,        //增加表单是否可见
                dialogFormVisible4Edit: false,   //编辑表单是否可见
                rules: {          //表单校验规则
                    tname: [{required: true, message: '分项工程不能为空', trigger: 'blur'}],
                    starttime: [{required: true, message: '开始时间不能为空', trigger: 'blur'}],
                    endtime: [{validator: checkendtime, trigger: 'blur'}]
                }
            }

        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();      //VUE对象初始化完成后调用分页查询方法，完成分页查询
        },
        methods: {

            subitemUpdate() {
                //进行表单校验
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {
                        //表单校验通过，可以提交数据
                        axios.post("/subitem/update", this.formData).then((res) => {
                            if (res.data.flag) {
                                //弹出成功提示信息
                                this.$message({
                                    type: '更新成功',
                                    message: res.data.message
                                });
                            } else {
                                //执行失败
                                this.$message.error(res.data.message);
                            }
                        }).finally(() => {
                            //不管成功还是失败，都调用分页查询方法
                            this.findPage();
                            //隐藏编辑窗口
                            this.dialogFormVisible4Edit = false;
                        });
                    } else {
                        //表单校验不通过
                        this.$message.error("表单数据校验失败!");
                        return false;
                    }
                });
            },
            subitemAdd() {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        console.log(this.form)
                        //表单校验通过，发生ajax请求，将录入的数据提交到后台进行处理
                        axios.post("/subitem/add", this.form).then((res) => {
                            //关闭新增窗口
                            this.dialogFormVisible = false;
                            if (res.data.flag) {//执行成功
                                //新增成功后，重新调用分页查询方法，查询出最新的数据
                                this.findPage();
                                //弹出提示信息
                                this.$message({
                                    message: res.data.message,
                                    type: '添加成功'
                                });
                            } else {//执行失败
                                //弹出提示
                                this.$message.error(res.data.message);
                            }
                        });
                    } else {
                        //校验不通过
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                        return false;
                    }
                });
            },
            //分页查询
            findPage() {
                //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                // 使用axios发送请求，请求控制层project下的all标注的方法
                // res及response，请求后的结果以json对象形式存储在response内

                axios.post("/subitem/all", param).then((res) => {
                    //解析Controller响应回的数据，为模型数据赋值
                    //将请求的结果复制给vm对象的data内的对象
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                });
            },
            // 重置表单
            resetForm() {
                this.form= {
                    sname: '',
                    starttime: '',
                    endtime: '',
                    option1: [],
                    pid: ''
                };//重置数据
            },

            handleAdd() {
                //弹出新增窗口
                this.dialogFormVisible = true;
                this.resetForm();
                axios.get("/project/findProjects").then((res) => {
                    if (res.data.flag) {
                        this.form.option1 = res.data.data;
                    } else {
                        //查询失败，弹出提示
                        this.$message.error(res.data.message);
                    }
                });
            },

            // 弹出编辑窗口
            handleUpdate(row) {
                //弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                //回显数据，发送ajax请求根据ID查询当前检查项数据，用于表单的数据回显
                axios.get("/subitem/findSubitemBySuid?suid=" + row.suid).then((res) => {
                    if (res.data.flag) {
                        //进行回显，基于VUE的数据绑定实现
                        this.formData = res.data.data;
                    } else {
                        //查询失败，弹出提示
                        this.$message.error(res.data.message);
                    }
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //设置最新的页码
                this.pagination.currentPage = currentPage;
                //重新调用findPage方法进行分页查询
                this.findPage();
            },
            subitemDelete(row) {    //row其实是一个json对象
                this.$confirm("你确定要删除当前数据吗？", "提示", {//确认框
                    type: 'warning'
                }).then(() => {
                    //用户点击确定按钮，发送ajax请求，将检查项ID提交到Controller进行处理
                    axios.get("/subitem/delete/" + row.suid).then((res) => {
                        console.log(res.data)
                        if (res.data.flag) {
                            //执行成功
                            //弹出成功提示信息
                            this.$message({
                                type: '删除成功',
                                message: res.data.message
                            });
                            //重新进行分页查询
                            this.findPage();
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '操作已取消'
                    });
                });
            }
        }
    })
</script>
</html>